import React from 'react';
import {
  Form,
  Input,
  Button,
  DatePicker,
  Select,
  Table,
  Icon,
  Radio,
  InputNumber,
  Checkbox,
  Modal,
  message,
  notification,
  Affix,
  Upload,
  Row,
  Col
} from 'antd';
import InnerTable from 'components/DBTable/InnerTable.js';
import ajax from 'utils/ajax';
import loginVerify from 'utils/loginVerify';


const FormItem = Form.Item;
const ButtonGroup = Button.Group;

/**
 * 内部表格组件
 */
class ArticleManageInnerTable extends InnerTable {

  currentMuseumId = {
    museumId : "",

  }

  setCurrentMuseumId = (museumId) =>{
    this.currentMuseumId.museumId = museumId;
  }

  getCurrentMuseumId = () =>{
    return this.currentMuseumId.museumId;
  }

  constructor(props) {
    super(props);
    console.log('props',props);
  }


  componentWillReceiveProps = (nextProps) => {
    // 其实传入的props和当前的props可能是一样的, 这个方法不会判断修改才触发
    // 要自己判断props是否有变化
    if(nextProps.requestParams && nextProps.requestParams.catId){
      this.setState({catId: nextProps.requestParams.catId})
    }
  }


  prepareToolBar = () =>{
    let isButtondisabled = false;

    if (this.state.selectedRowKeys.length > 0) {  // 是否选择
      isButtondisabled = true;
    }

    if (this.state.selectedRowKeys.length > 1) {  // 是否选择了多项
      isButtondisabled = false;
    }


    var items = [];
    let selectRows = this.state.selectedRows[0];
    if(!selectRows){ //如果没有选择
      items.push(<Button key='roleAssignBtn' disabled={true}>
        <Icon type="user"/>上架
      </Button>)
    }else{
      // console.log(selectRows.status);
      if (selectRows.status == 1) {
        items.push(<Button key='roleAssignBtn' disabled={false} onClick={e=>this.onChangeState(2)}>
          <Icon type="user"/>下架
        </Button>)
      } else {
        items.push(<Button key='roleAssignBtn' disabled={false} onClick={e=>this.onChangeState(1)}>
          <Icon type="user"/>上架
        </Button>)
      }

    }

    return items;
  }

  onChangeState = (status) =>{
    Modal.confirm({
      title:  '确认' + (status == 2 ? '下架?' : '上架?'),
      content: `${this.state.selectedRows[0].title}`,
      // 这里注意要用箭头函数, 否则this不生效
      onOk: () => {
        this.handleChangeState(this.state.selectedRows[0], status);
      },
    });
  }

  handleChangeState = (obj, status) =>{
    let _this = this;
    ajax.post({
      url: '/articlemanage/updateStatus',
      params: {id : obj.id, status : status},
      success: function (res) {
        if (res.errorCode == 200) {
          message.success('修改成功');
          _this.props.refresh(); //刷新列表
        } else if (res.errorCode == 300) {
          notification.error({
            message: '出错啦!',
            description: res.moreInfo,
            duration: 0,
          });
        }
      }
    })
  }

  preDealTableData = (obj,newObj) =>{
    if (newObj && newObj.museumId){
      this.setCurrentMuseumId(newObj.museumId);
    }
  }
  onClickInsert = () =>{
    window.location.href = "/cms/manage/index#/cms/manage/ArticleManage/articleEdit?catId=" + this.state.catId;
  }
  handleData = (obj) => {
    if (obj.actionState == this.modalActionState.Insert
      || obj.actionState == this.modalActionState.Update) {
        obj.params.catId=this.props.requestParams.catId;
    }
    return obj;
  }

}

export default ArticleManageInnerTable;
